Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ----------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts -------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
        p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 296)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.92588 12.92751 12.92918 12.93088 12.93258 12.93429 12.93599 12.93767
##   [9] 12.93931 12.94092 12.94248 12.94397 12.94539 12.94672 12.94796 12.94909
##  [17] 12.95010 12.95099 12.95174 12.95234 12.95278 12.95305 12.95317 12.95320
##  [25] 12.95313 12.95297 12.95272 12.95238 12.95197 12.95149 12.95094 12.95033
##  [33] 12.94965 12.94893 12.94815 12.94733 12.94647 12.94558 12.94465 12.94370
##  [41] 12.94273 12.94175 12.94075 12.93975 12.93874 12.93774 12.93674 12.93576
##  [49] 12.93480 12.93386 12.93285 12.93170 12.93041 12.92899 12.92746 12.92581
##  [57] 12.92408 12.92225 12.92036 12.91839 12.91637 12.91431 12.91221 12.91009
##  [65] 12.90795 12.90581 12.90368 12.90156 12.89948 12.89742 12.89542 12.89348
##  [73] 12.89160 12.88980 12.88809 12.88649 12.88499 12.88361 12.88189 12.87941
##  [81] 12.87623 12.87240 12.86798 12.86304 12.85762 12.85179 12.84560 12.83912
##  [89] 12.83240 12.82550 12.81848 12.81139 12.80430 12.79726 12.79034 12.78358
##  [97] 12.77705 12.77080 12.76490 12.75940 12.75437 12.74985 12.74590 12.74259
## [105] 12.73998 12.73812 12.73637 12.73411 12.73138 12.72827 12.72481 12.72109
## [113] 12.71714 12.71304 12.70885 12.70463 12.70043 12.69632 12.69236 12.68861
## [121] 12.68513 12.68198 12.67921 12.67690 12.67510 12.67388 12.67328 12.67338
## [129] 12.67424 12.67590 12.67844 12.68192 12.68639 12.69279 12.70176 12.71292
## [137] 12.72589 12.74030 12.75576 12.77190 12.78834 12.80471 12.82062 12.83569
## [145] 12.84956 12.86183 12.87214 12.88245 12.89475 12.90872 12.92403 12.94035
## [153] 12.95736 12.97471 12.99210 13.00919 13.02564 13.04114 13.05535 13.06795
## [161] 13.07860 13.09031 13.10585 13.12455 13.14575 13.16880 13.19303 13.21778
## [169] 13.24240 13.26621 13.28857 13.30882 13.32628 13.34030 13.35022 13.35848
## [177] 13.36783 13.37811 13.38916 13.40081 13.41290 13.42526 13.43773 13.45014
## [185] 13.46233 13.47413 13.48537 13.49590 13.50555 13.51415 13.52155 13.52756
## [193] 13.53203 13.53480 13.53569 13.53455 13.53121 13.52551 13.51739 13.50709
## [201] 13.49483 13.48087 13.46543 13.44874 13.43106 13.41261 13.39364 13.37437
## [209] 13.35504 13.33590 13.31717 13.29910 13.27927 13.25549 13.22831 13.19831
## [217] 13.16604 13.13209 13.09701 13.06136 13.02572 12.99065 12.95671 12.92448
## [225] 12.89451 12.86737 12.84017 12.80997 12.77732 12.74275 12.70679 12.67000
## [233] 12.63289 12.59601 12.55990 12.52509 12.49211 12.46152 12.43383 12.40959
## [241] 12.38691 12.36365 12.33996 12.31599 12.29191 12.26787 12.24403 12.22055
## [249] 12.19757 12.17527 12.15379 12.13329 12.11394 12.09587 12.07849 12.06113
## [257] 12.04385 12.02672 12.00981 11.99319 11.97691 11.96106 11.94569 11.93088
## [265] 11.91668 11.90318 11.89043 11.87851 11.86719 11.85623 11.84564 11.83544
## [273] 11.82566 11.81631 11.80741 11.79899 11.79106 11.78364 11.77675 11.77042
## [281] 11.76467 11.75950 11.75503 11.75132 11.74833 11.74602 11.74437 11.74333
## [289] 11.74288 11.74297 11.74357 11.74465 11.74617 11.74810 11.75040 11.75303
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 296)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.57841 12.57616 12.57399 12.57190 12.56988 12.56794 12.56606 12.56424
##   [9] 12.56249 12.56080 12.55916 12.55758 12.55604 12.55455 12.55310 12.55169
##  [17] 12.55032 12.54898 12.54767 12.54639 12.54513 12.54389 12.54266 12.54145
##  [25] 12.54025 12.53906 12.53788 12.53669 12.53550 12.53430 12.53305 12.53179
##  [33] 12.53050 12.52920 12.52789 12.52658 12.52527 12.52397 12.52269 12.52143
##  [41] 12.52020 12.51901 12.51786 12.51676 12.51571 12.51472 12.51379 12.51294
##  [49] 12.51217 12.51148 12.51088 12.51038 12.50998 12.50969 12.50952 12.50947
##  [57] 12.50955 12.50966 12.50973 12.50976 12.50977 12.50975 12.50973 12.50970
##  [65] 12.50969 12.50970 12.50974 12.50982 12.50994 12.51013 12.51038 12.51072
##  [73] 12.51114 12.51166 12.51228 12.51303 12.51390 12.51490 12.51605 12.51736
##  [81] 12.51884 12.52048 12.52232 12.52434 12.52657 12.52874 12.53058 12.53212
##  [89] 12.53340 12.53444 12.53528 12.53593 12.53643 12.53682 12.53711 12.53733
##  [97] 12.53752 12.53771 12.53792 12.53819 12.53853 12.53899 12.53959 12.54035
## [105] 12.54131 12.54250 12.54395 12.54568 12.54772 12.55011 12.55287 12.55603
## [113] 12.55962 12.56311 12.56601 12.56840 12.57037 12.57199 12.57335 12.57454
## [121] 12.57563 12.57671 12.57786 12.57916 12.58070 12.58256 12.58482 12.58757
## [129] 12.59088 12.59484 12.59953 12.60504 12.61144 12.61998 12.63154 12.64566
## [137] 12.66188 12.67974 12.69879 12.71857 12.73862 12.75848 12.77769 12.79581
## [145] 12.81236 12.82689 12.83894 12.85188 12.86891 12.88938 12.91262 12.93798
## [153] 12.96477 12.99235 13.02004 13.04719 13.07313 13.09720 13.11873 13.13707
## [161] 13.15154 13.16557 13.18266 13.20224 13.22371 13.24653 13.27009 13.29385
## [169] 13.31721 13.33961 13.36047 13.37922 13.39528 13.40808 13.41705 13.42394
## [177] 13.43085 13.43770 13.44441 13.45091 13.45713 13.46298 13.46839 13.47328
## [185] 13.47758 13.48120 13.48408 13.48614 13.48729 13.48747 13.48660 13.48460
## [193] 13.48139 13.47690 13.47105 13.46376 13.45496 13.44458 13.43162 13.41545
## [201] 13.39649 13.37516 13.35190 13.32714 13.30129 13.27480 13.24808 13.22156
## [209] 13.19568 13.17086 13.14752 13.12610 13.10312 13.07535 13.04354 13.00844
## [217] 12.97081 12.93141 12.89099 12.85032 12.81014 12.77121 12.73429 12.70014
## [225] 12.66951 12.64315 12.61745 12.58867 12.55742 12.52433 12.49001 12.45507
## [233] 12.42014 12.38581 12.35271 12.32146 12.29266 12.26694 12.24490 12.22717
## [241] 12.21151 12.19540 12.17905 12.16266 12.14642 12.13054 12.11521 12.10063
## [249] 12.08699 12.07451 12.06338 12.05379 12.04595 12.04005 12.03529 12.03078
## [257] 12.02663 12.02293 12.01978 12.01727 12.01551 12.01460 12.01463 12.01570
## [265] 12.01791 12.02136 12.02614 12.03236 12.03970 12.04780 12.05670 12.06641
## [273] 12.07697 12.08840 12.10072 12.11397 12.12817 12.14335 12.15953 12.17674
## [281] 12.19501 12.21436 12.23495 12.25690 12.28016 12.30466 12.33036 12.35720
## [289] 12.38512 12.41408 12.44402 12.47489 12.50663 12.53919 12.57252 12.60656
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 296)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.84845 11.85222 11.85607 11.85997 11.86392 11.86789 11.87186 11.87583
##   [9] 11.87978 11.88368 11.88753 11.89131 11.89499 11.89858 11.90204 11.90537
##  [17] 11.90855 11.91155 11.91438 11.91700 11.91941 11.92158 11.92351 11.92517
##  [25] 11.92655 11.92763 11.92840 11.92885 11.92894 11.92874 11.92830 11.92764
##  [33] 11.92677 11.92570 11.92445 11.92303 11.92145 11.91974 11.91789 11.91593
##  [41] 11.91387 11.91173 11.90950 11.90722 11.90490 11.90254 11.90016 11.89777
##  [49] 11.89539 11.89304 11.89072 11.88844 11.88623 11.88410 11.88205 11.88011
##  [57] 11.87829 11.87617 11.87336 11.86993 11.86592 11.86139 11.85638 11.85095
##  [65] 11.84516 11.83905 11.83268 11.82610 11.81936 11.81252 11.80562 11.79872
##  [73] 11.79188 11.78515 11.77857 11.77220 11.76609 11.76030 11.75488 11.74988
##  [81] 11.74535 11.74135 11.73792 11.73513 11.73301 11.73049 11.72653 11.72125
##  [89] 11.71477 11.70723 11.69875 11.68946 11.67949 11.66896 11.65800 11.64674
##  [97] 11.63531 11.62383 11.61242 11.60122 11.59036 11.57996 11.57014 11.56104
## [105] 11.55278 11.54549 11.53929 11.53432 11.53070 11.52855 11.52801 11.52920
## [113] 11.53225 11.53706 11.54336 11.55105 11.56001 11.57014 11.58133 11.59346
## [121] 11.60643 11.62012 11.63442 11.64923 11.66444 11.67993 11.69559 11.71131
## [129] 11.72699 11.74252 11.75777 11.77265 11.78704 11.80360 11.82460 11.84938
## [137] 11.87727 11.90759 11.93968 11.97285 12.00644 12.03978 12.07220 12.10302
## [145] 12.13157 12.15719 12.17919 12.20124 12.22702 12.25591 12.28726 12.32045
## [153] 12.35486 12.38984 12.42477 12.45902 12.49195 12.52295 12.55136 12.57658
## [161] 12.59796 12.61818 12.64011 12.66338 12.68761 12.71246 12.73756 12.76254
## [169] 12.78704 12.81069 12.83314 12.85402 12.87296 12.88961 12.90359 12.91637
## [177] 12.92954 12.94300 12.95663 12.97031 12.98393 12.99736 13.01049 13.02321
## [185] 13.03539 13.04692 13.05768 13.06755 13.07642 13.08417 13.09068 13.09584
## [193] 13.09952 13.10162 13.10201 13.10057 13.09720 13.09177 13.08440 13.07540
## [201] 13.06485 13.05287 13.03957 13.02506 13.00944 12.99283 12.97533 12.95705
## [209] 12.93810 12.91859 12.89863 12.87832 12.85455 12.82474 12.78984 12.75076
## [217] 12.70844 12.66382 12.61781 12.57136 12.52539 12.48084 12.43864 12.39971
## [225] 12.36499 12.33541 12.30643 12.27341 12.23711 12.19831 12.15780 12.11635
## [233] 12.07474 12.03374 11.99413 11.95669 11.92219 11.89141 11.86514 11.84413
## [241] 11.82582 11.80722 11.78848 11.76978 11.75128 11.73314 11.71553 11.69861
## [249] 11.68256 11.66753 11.65368 11.64119 11.63022 11.62093 11.61255 11.60428
## [257] 11.59622 11.58850 11.58121 11.57449 11.56845 11.56319 11.55884 11.55550
## [265] 11.55329 11.55233 11.55273 11.55461 11.55757 11.56117 11.56546 11.57048
## [273] 11.57627 11.58288 11.59034 11.59869 11.60799 11.61828 11.62958 11.64196
## [281] 11.65544 11.67008 11.68605 11.70346 11.72224 11.74233 11.76366 11.78616
## [289] 11.80978 11.83443 11.86006 11.88659 11.91397 11.94212 11.97097 12.00048
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")